var LLParser = require('./LLParser');
var parsePath = require('./parsePath');
var r = null;
var o = () => null !== r ? r : r = undefined !== __wxConfig && 'devtools' === __wxConfig.platform || Math.random() < 0.0001;
var s = ' \n\r\t\f';
var l = null;
exports.parseMultiPaths = function (e) {
    l || (l = LLParser.create({
        MULTIPLE_PATHS: [
            {
                id: 'arrayConcat',
                states: [
                    'MULTIPLE_PATHS',
                    ',',
                    'SINGLE_PATH'
                ]
            },
            {
                id: 'array',
                states: ['SINGLE_PATH']
            }
        ],
        SINGLE_PATH: [
            {
                id: 'arrayConcat',
                states: [
                    'SINGLE_PATH',
                    '.',
                    'VAR_NAME'
                ]
            },
            {
                id: 'arrayConcat',
                states: [
                    'SINGLE_PATH',
                    '[',
                    'INT',
                    ']'
                ]
            },
            {
                id: 'array',
                states: ['VAR_NAME']
            },
            {
                id: '_jump',
                states: [
                    'SINGLE_PATH',
                    s
                ]
            }
        ],
        VAR_NAME: [
            {
                id: '_blank',
                states: [
                    s,
                    'VAR_NAME'
                ]
            },
            {
                id: '_jump',
                states: [
                    'VAR_NAME',
                    s
                ]
            },
            {
                id: '_raw',
                states: [
                    '*',
                    '*'
                ]
            },
            {
                id: '_raw',
                states: [
                    '_a-zA-Z$',
                    'VAR_NAME_AFTER'
                ]
            }
        ],
        VAR_NAME_AFTER: [
            {
                id: '_raw',
                states: [
                    '_a-zA-Z0-9$',
                    'VAR_NAME_AFTER'
                ]
            },
            {
                id: '_raw',
                states: ['NULL']
            }
        ],
        INT: [
            {
                id: '_blank',
                states: [
                    s,
                    'INT'
                ]
            },
            {
                id: '_jump',
                states: [
                    'INT',
                    s
                ]
            },
            {
                id: 'toNumber',
                states: [
                    '0-9',
                    'INT_AFTER'
                ]
            }
        ],
        INT_AFTER: [
            {
                id: '_raw',
                states: [
                    '0-9',
                    'INT_AFTER'
                ]
            },
            {
                id: '_raw',
                states: ['NULL']
            }
        ]
    }, {
        arrayConcat: function (e) {
            e[0].push(e[2]);
            return e[0];
        },
        array: function (e) {
            return e;
        },
        toNumber: function (e) {
            return parseInt(e[0] + e[1], 10);
        }
    }));
    var t = l.parse('MULTIPLE_PATHS', e);
    if (o())
        try {
            var i = null;
            try {
                i = parsePath.parseMultiPaths(e);
            } catch (e) {
            }
            if (JSON.stringify(t) !== JSON.stringify(i)) {
                console.warn('The observer path "' + e + '" is illegal. It may fail in future.');
                Foundation.emitFrameworkError(new Error('Observers data-path parsing fail: ' + e));
            };
        } catch (e) {
        }
    return t;
};
exports.parseSinglePath = function (e) {
    var t = e.length;
    var i = [];
    var n = '';
    var r = 0;
    var s = false;
    var l = false;
    for (var d = 0; d < t; d++) {
        var A = e[d];
        if ('\\' === A)
            d + 1 < t && ('.' === e[d + 1] || '[' === e[d + 1] || ']' === e[d + 1] || '\\' === e[d + 1]) ? (n += e[d + 1], d++) : n += '\\';
        else if ('.' === A)
            n && (i.push(n), n = '');
        else if ('[' === A) {
            if (n) {
                i.push(n);
                n = '';
            };
            if (0 === i.length)
                throw new Error('The path string should not start with []: ' + e);
            l = true;
            s = false;
        } else if (']' === A) {
            if (!s)
                throw new Error('There should be digits inside [] in the path string: ' + e);
            l = false;
            i.push(r);
            r = 0;
        } else if (l) {
            if (A < '0' || A > '9')
                throw new Error('Only digits (0-9) can be put inside [] in the path string: ' + e);
            s = true;
            r = 10 * r + A.charCodeAt(0) - 48;
        } else
            n += A;
    }
    n && i.push(n);
    if (0 === t)
        throw new Error('The path string should not be empty');
    if (o())
        try {
            var c = null;
            try {
                c = parsePath.parseSinglePath(e);
            } catch (e) {
            }
            if (JSON.stringify(i) !== JSON.stringify(c)) {
                console.warn('The setData data-path "' + e + '" is illegal. It may fail in future.');
                Foundation.emitFrameworkError(new Error('SetData data-path parsing fail: ' + e));
            };
        } catch (e) {
        }
    return i;
};
